package com.ims.business.controller;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.assertj.core.util.Lists;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.ims.business.constant.BusCons;
import com.ims.common.matatype.Dto;
import com.ims.common.matatype.Dtos;
import com.ims.common.util.FileOperation;
import com.ims.common.util.IMSUtil;
import com.ims.common.util.IdUtil;
import com.ims.common.util.R;
import com.ims.common.web.BaseController;
import com.ims.system.util.CacheCxt;
/**
 * 
 * 类名:com.ims.business.controller.FileController
 * 描述:文件操作控制类
 * 编写者:陈骑元
 * 创建时间:2019年7月9日 上午8:20:26
 * 修改说明:
 */
@Controller
@RequestMapping("/file")
public class FileController extends BaseController{
	
	
	/**
	 * 显示图片
	 * @param response
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "showImage")
	public void showImage(String fileName,HttpServletRequest request,HttpServletResponse response) {
		String folderPath=CacheCxt.getParamValue(BusCons.SAVE_ROOT_PATH_KEY	);
		String filePath=folderPath+File.separator+BusCons.SCOLL_IMAGE+File.separator+fileName;
		FileOperation.downloadFile(request, response,  filePath, fileName);
	}
	/**
	 * 
	 * 简要说明： 新增信息保存 
	 * 编写者：陈骑元
	 * 创建时间：2019-02-23
	 * @param 说明
	 * @return 说明
	 */
	@PostMapping("uploadImage")
	@ResponseBody
	public R uploadImage(@RequestParam(value = "file", required = false) MultipartFile file,
			HttpServletRequest request,HttpServletResponse response) {
		if(IMSUtil.isNotEmpty(file)&&file.getSize()>0){
			
			try {
				String rootPath=CacheCxt.getParamValue(BusCons.SAVE_ROOT_PATH_KEY	);
				String folderPath=rootPath+File.separator+BusCons.SCOLL_IMAGE;
				String imageName=file.getOriginalFilename();
				String imageFileName=IdUtil.uuid()+"."+FileOperation.getFileType(imageName);
				FileOperation.createFolder(folderPath); 
				File targetFile = new File(folderPath,imageFileName); 
				file.transferTo(targetFile);
				return R.ok().put("imageFileName", imageFileName);
			} catch (Exception e) {
				
				e.printStackTrace();
			} 
			return R.error("上传图片文件失败");
			 
		}
       
		return R.error("没有收到文件流");
	}
	/**
	 * 
	 * 简要说明： 多文件上传
	 * 编写者：陈骑元
	 * 创建时间：2019-02-23
	 * @param 说明
	 * @return 说明
	 */
	@PostMapping("uploadMoreImage")
	@ResponseBody
	public R uploadMoreImage(HttpServletRequest request,HttpServletResponse response) {
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
				request.getSession().getServletContext());
		
		try {
			if (multipartResolver.isMultipart(request)) {
				MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
				Iterator<String> fileForms = multipartRequest.getFileNames();
				String rootPath = CacheCxt.getParamValue(BusCons.SAVE_ROOT_PATH_KEY);
				String folderPath = rootPath + File.separator + BusCons.SCOLL_IMAGE;
				List<Dto> dataList=Lists.newArrayList();
				while (fileForms.hasNext()) {
					String fileForm = fileForms.next();// 文件提交表单域名称
					List<MultipartFile> files = multipartRequest.getFiles(fileForm);
					for (int i = 0; i < files.size(); i++) {
						MultipartFile file = files.get(i);
						if (file != null && file.getSize() > 0) { // 如果存在上传文件

							String imageName = file.getOriginalFilename();
							String imageFileName = IdUtil.uuid() + "." + FileOperation.getFileType(imageName);
							FileOperation.createFolder(folderPath);
							File targetFile = new File(folderPath, imageFileName);
							file.transferTo(targetFile);
							Dto dataDto=Dtos.newDto();
							dataDto.put("imageFileName", imageFileName);
							dataList.add(dataDto);
						}
					}
				}
				return R.toList(dataList);

			} 
		} catch (Exception e) {
			return R.error("上传文件失败");
		}
		return R.error("没有收到文件流");
	}
}
